package algorithms;

import java.util.Arrays;

/**
 * 冒泡排序
 */
public class BubbleSort {

    public static void main(String[] args) {
//        int[] list = {5, 3, 2, 6, 1, 8, 9, 0};
        int[] list = {5, 3, 2, 6, 1, 8, 8, 2};
//        int[] list = {1, 2, 5, 7, 8};
        System.out.println("排序前：\n" + Arrays.toString(list));
        int[] sort = sort3(list);
        System.out.println("排序后：");
        System.out.println(Arrays.toString(sort));
    }

    public static int[] sort3(int[] arr) {
        int count = 0;
        int flag = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                System.out.println(++count);
                int current = arr[j];
                int next = arr[j + 1];
                if (current > next) {
                    int temp = current;
                    arr[j] = next;
                    arr[j + 1] = temp;
                    flag = 1;
                }
                if (flag == 0) {
                    return arr;
                }
            }
        }
        return arr;
    }

    public static int[] sort2(int[] list) {
        int count = 0;
        for (int i = 0; i < list.length - 1; i++) {
            for (int j = 0; j < list.length - 1 - i; j++) {
                System.out.println(++count);
                int curItem = list[j];
                int nextItem = list[j + 1];
                int temp = 0;
                if (curItem > nextItem) {
                    //交换值
                    temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(list));
        return list;
    }

    public static int[] sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int current = arr[0];
            for (int j = 1; j < arr.length; j++) {
                int item = arr[j];
                if (current > item) {
                    arr[j] = current;
                    arr[j - 1] = item;
                } else {
                    arr[j - 1] = current;
                    current = item;
                }
            }
        }
        return arr;
    }

    /**
     * 加上序列状态  提高排序效率
     * 利用flag做标记。如果在本轮排序中，元素有交换，则说明数列无序，如果没有交换，则说明数列已然有序，直接返回。
     *
     * @param arr
     * @return
     */
    public static int[] sort1(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int current = arr[0];
            int flag = 0;
            for (int j = 1; j < arr.length; j++) {
                int item = arr[j];
                if (current > item) {
                    arr[j] = current;
                    arr[j - 1] = item;
                    flag = 1;
                } else {
                    arr[j - 1] = current;
                    current = item;
                    flag = 1;
                }
                if (flag == 0) {
                    return arr;
                }
            }
        }
        return arr;
    }
}


